package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleManageDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = RoleManageService.class)
@Transactional
public class RoleManageServiceImpl implements RoleManageService{


    @Autowired
    RoleManageDao roleManageDao;

    @Autowired
    PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = roleManageDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Result findPermission() {
        try {
            List<Permission> permissionList=permissionDao.findPermission();
            return new Result(true, MessageConstant.GET_PERSSION_LIST_SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_PERSSION_LIST_FAIL);
        }
    }

    @Override
    public void addRole(Integer[] permissionIds, Role role) {
        roleManageDao.addRole(role);
        Integer id = role.getId();
        for (Integer permissionId : permissionIds) {

            HashMap<String, Integer> map = new HashMap<>();
            map.put("permissionId",permissionId);
            map.put("roleId",id);
            roleManageDao.addRoleIdAndPerssionId(map);
        }
    }

    @Override
    public Role getRole(int _id) {
        Role role = roleManageDao.findById(_id);
       return role;
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleManageDao.findPermissionIdsByRoleId(id);
    }


    @Override
    public void edit(Role role, Integer[] permissionIds) {
        //通过获取到的role信息中的id,将关联表中的所有权限信息删除
        Integer id = role.getId();
        permissionDao.delete(id);

        //将获取到的权限数组完成添加
        setRoleAndPermissionIds(permissionIds,id);

        //将获取到的role信息完成修改
        roleManageDao.edit(role);



    }

    public void setRoleAndPermissionIds(Integer[] permissionIds,Integer roleId){
        if(permissionIds!=null&&permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                permissionDao.add(map);
            }
        }
    }

    @Override
    public void delete(int id) {
        roleManageDao.deletePremission(id);
        roleManageDao.deleteRole(id);
    }
}
